package com.yingpai.fitness.dvp_2_0.main.activity.shop;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.adapter.shop.ShopClassifyAdapter;
import com.yingpai.fitness.dvp_2_0.main.bean.ShopClassifyBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomGridLayoutManage;
import com.yingpai.fitness.dvp_2_0.main.widget.ParentRecyclerView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/1/2.
 */

public class ShopClassifyActivity extends CommonActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.shop_sale_fragment_rv_list)
    ParentRecyclerView shop_sale_fragment_rv_list;
    @BindView(R.id.swiperefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    private ShopClassifyAdapter mAdapter;
    private String id;
    private int pageNum = 1;
    private int pageSize = 10;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_shop_classify);
    }

    @Override
    protected void initView() {
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.common_color);

        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            id = bundle.getString("id");
            title = bundle.getString("title");
            setTitle(title);
        }

        shop_sale_fragment_rv_list.setLayoutManager(new CustomGridLayoutManage(context, 2));
        mAdapter = new ShopClassifyAdapter(R.layout.shop_sale_list_item);
        mAdapter.setOnLoadMoreListener(this,shop_sale_fragment_rv_list);
        mAdapter.setOnItemChildClickListener(this);
        shop_sale_fragment_rv_list.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        pageNum = 1;
        requestData();
    }

    private void requestData() {
        showDialog(this,null);
        HashMap<String,Object> httpParams = new HashMap();
        httpParams.put("tagId", id);
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        finalOkGo.requestEncrypt(RequestType.GETENCRYPT, Contacts.HOST_SERVER + Url.GET_MERCHANDISE_LIST,
                httpParams, Url.GET_MERCHANDISE_LIST, new Callback<ShopClassifyBean>() {
            @Override
            public void onSuccess(ShopClassifyBean shopClassifyBean) {
                if(shopClassifyBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    if (pageNum - 1 == 1) {
                        if(shopClassifyBean.getData().getTotal()>0){
                            empty.setVisibility(View.GONE);
                            mAdapter.setNewData(shopClassifyBean.getData().getRows());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        mAdapter.addData(shopClassifyBean.getData().getRows());
                    }
                    swipeRefreshLayout.setRefreshing(false);
                    mAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (shopClassifyBean.getData().getTotal() % pageSize == 0) {
                        pagecount = shopClassifyBean.getData().getTotal() / pageSize;
                    } else {
                        pagecount = shopClassifyBean.getData().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        mAdapter.loadMoreEnd();
                    }else{
                        mAdapter.loadMoreComplete();
                        mAdapter.setEnableLoadMore(true);
                    }
                }else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(shopClassifyBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
        }
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        requestData();
    }

    @Override
    public void onLoadMoreRequested() {
        requestData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.listitem_zone:
                Bundle bundle = new Bundle();
                bundle.putInt("id",mAdapter.getItem(position).getId());
                startActivity(ShopDetailActivity.class,bundle);
                break;
        }
    }
}
